package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author DuXiangJie
 * @Date 2020-04-15
 * @Version v1.0
 * @Description 角色管理
 */


@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

//删除角色
    @RequestMapping("delById")
    public Result delById(Integer id){
        try {
            roleService.delById(id);
            return new Result(true,MessageConst.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.DELETE_ROLE_FAIL);

        }
    }


//    根据ID查询角色
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_ROLE_FAIL);
        }
    }

//查询角色关联的权限
    @RequestMapping("findRoleIdByPermissionId")

    public Result findRoleIdByPermissionId(Integer id){
        try {
            List<Integer> roleIdByPermissionId = roleService.findRoleIdByPermissionId(id);
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,roleIdByPermissionId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);

        }
    }
//查看角色关联的菜单
    @RequestMapping("findRoleIdByMenuId")
    public Result findRoleIdByMenuId(Integer id){
        try {
            List<Integer> menu = roleService.findRoleIdByMenuId(id);
            return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_MENU_FAIL);

        }
    }

    //    新增角色
    @RequestMapping("add")
    public Result add(@RequestBody Role role,Integer[] perm,Integer[] menu){


        try {
            roleService.add(role,perm,menu);
            return new Result(true,MessageConst.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_ROLE_FAIL);

        }


    }

//    编辑角色
    @RequestMapping("edit")
    public Result edit(@RequestBody Role role,Integer[] perm,Integer[] menu){
        try {
            roleService.edit(role,perm,menu);
            return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_ROLE_FAIL);

        }
    }



//    分页查找
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            System.out.println("角色查询："+pageResult);
            return  new Result(true, MessageConst.QUERY_ROLE_SUCCESS,pageResult);

        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConst.QUERY_ROLE_FAIL);

        }

    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConst.ACTION_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ACTION_FAIL);
        }
    }
}
